using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Forum
{
    [Serializable()]
    public class ForumModerators : Csla.BusinessBase<ForumModerators>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _forumID = Guid.Empty;
        private Guid _UserID = Guid.Empty;

        [System.ComponentModel.DataObjectField(true, false)]
        public Guid ForumID
        {
            get
            {
                CanReadProperty("ForumID", true);
                return _forumID;
            }
            set
            {
                CanWriteProperty("ForumID", true);
                if(!_forumID.Equals(value))
                {
                    _forumID = value;
                    PropertyHasChanged("ForumID");
                }
            }
        }

        public Guid UserID
        {
            get
            {
                CanReadProperty("UserID", true);
                return _UserID;
            }
            set
            {
                CanWriteProperty("UserID", true);
                if(!_UserID.Equals(value))
                {
                    _UserID = value;
                    PropertyHasChanged("UserID");
                }
            }
        }

        protected override object GetIdValue()
        {
            return _forumID;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
        }

        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in Forum
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ForumViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in Forum
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ForumAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in Forum
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ForumEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in Forum
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("ForumDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods

        private ForumModerators()
        { /* require use of factory method */
        }

        public static ForumModerators NewForumModerators()
        {
            if(!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a ForumModerators");
            return DataPortal.Create<ForumModerators>();
        }

        public static ForumModerators GetForumModerators(Guid forumID)
        {
            if(!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a ForumModerators");
            return DataPortal.Fetch<ForumModerators>(new Criteria(forumID));
        }

        public static void DeleteForumModerators(Guid forumID, Guid userID)
        {
            if(!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a ForumModerators");
            DataPortal.Delete(new Criteria(forumID, userID));
        }

        public override ForumModerators Save()
        {
            if(IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a ForumModerators");
            else if(IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a ForumModerators");
            else if(!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a ForumModerators");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid ForumID;
            public Guid UserID;

            public Criteria(Guid forumID)
            {
                this.ForumID = forumID;
            }

            public Criteria(Guid forumID, Guid userID)
            {
                this.ForumID = forumID;
                this.UserID = userID;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@ForumID", criteria.ForumID);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetForumModeratorsByForumID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            dr.Read();
            _UserID = dr.GetGuid("UserID");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertForumModerators.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@ForumID", _forumID);
            DAL.AddParameter("@UserID", _UserID);

        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        protected override void DataPortal_Update()
        {
            if(base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdateForumModerators.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@ForumID", _forumID);
            DAL.AddParameter("@UserID", _UserID);
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_forumID));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);

            DAL.ExecuteNonQuery(Procs.DeleteForumModerators.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@ForumID", criteria.ForumID);
            DAL.AddParameter("@UserID", criteria.UserID);
        }
        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}
